package com.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.IdWorker;
import com.example.common.R;
import com.example.domain.AddressBook;
import com.example.service.AddressBookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@ResponseBody
@RequestMapping("/addressBook")
public class AddressBookController {
@Autowired
private AddressBookService addressBookService;
    @GetMapping("/userPage")//查找的最新订单，要修改
    public R<Page> userPage(int page, int pageSize, HttpSession session)
    {
        Page<AddressBook> p=new Page<>(page,pageSize);
        int pageCount=(page-1)*pageSize;
        Long user = (Long) session.getAttribute("user");
        List<AddressBook> selectall = addressBookService.selectall(pageCount, pageSize, user);
        p.setRecords(selectall);
        int getcount = addressBookService.getcount(user);
        p.setTotal(getcount);
        return R.success(p);
    }

    @GetMapping("/list")
    private R<List<AddressBook>> list(HttpSession session)
    {
        Long user = (Long) session.getAttribute("user");
        List<AddressBook> list = addressBookService.list(user);
        return R.success(list);
    }

    @PutMapping("/default")
    public R<String> setdefault(@RequestBody AddressBook addressBook,HttpSession session)
    {
        Long user = (Long) session.getAttribute("user");
        addressBookService.setstatusall(user);
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBook.setUpdateUser(user);
        addressBookService.setstatusone(addressBook);
        return R.success("默认地址修改完成");
    }

    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook,HttpSession session)
    {
        IdWorker idWorker=new IdWorker();
        addressBook.setId(idWorker.nextId());
        Long user = (Long) session.getAttribute("user");
        addressBook.setUserId(user);
        addressBook.setCreateTime(LocalDateTime.now());
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBook.setCreateUser(user);
        addressBook.setUpdateUser(user);
        addressBook.setIsDeleted(0);
        addressBook.setIsDefault(0);
        log.info("save before");
        log.info(addressBook.toString());
        addressBookService.save(addressBook);
        log.info("save after");
        return R.success("保存成功");
    }

    @GetMapping("/{id}")
    public R<AddressBook> alert(@PathVariable Long id,HttpSession session)
    {
        Long user = (Long) session.getAttribute("user");
        AddressBook alert = addressBookService.alert(id);
        return R.success(alert);
    }

    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook,HttpSession session)
    {
        Long user = (Long) session.getAttribute("user");
        addressBook.setUpdateUser(user);
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBookService.update(addressBook);
        return R.success("修改成功");
    }

    @DeleteMapping
    public R<String> delete(Long ids)
    {
        addressBookService.delete(ids);
        return R.success("删除成功");
    }

    @GetMapping("/default")
    public R<AddressBook> getdefault(HttpSession session)
    {
        Long user = (Long) session.getAttribute("user");
        AddressBook getdefault = addressBookService.getdefault(user);
        return R.success(getdefault);
    }
}
